package com.summer.data.demo.service;

import com.summer.data.Jql;
import com.summer.data.rs.datatype.ObjectPage;
import com.summer.data.demo.tables.StudentTable;
import com.summer.data.spring.SessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {

    @Autowired
    SessionTemplate sessionTemplate;

    StudentTable stuTable = new StudentTable();

    /**
     * 添加学生
     * @param studentId 学生id
     * @param studentName 学生姓名
     * @return 插入记录数量
     */
    public Integer addStudent(String studentId, String studentName) {
        StudentTable stu = new StudentTable();
        stu.resId.setValue(studentId);
        stu.studentName.setValue(studentName);

        return sessionTemplate.insert(stu);
    }

    public Integer updateStudent(String studentId, String studentName) {
        StudentTable stu = new StudentTable();
        stu.studentName.setValue(studentName);

        return sessionTemplate.update(stu, stu.resId.eq(studentId));
    }

    public Integer delStudent(String studentId) {
        return sessionTemplate.delete(StudentTable.class, stuTable.resId.eq(studentId));
    }

    /**
     * 无条件单表查询
     * @return
     */
    public List<StudentTable> listStudent() {
        return sessionTemplate.query(StudentTable.class);
    }

    /**
     * 无条件单表查询单条数据
     * @return
     */
    public StudentTable getStudentById(String studentId) {
        return sessionTemplate.find(StudentTable.class, stuTable.resId.eq(studentId));
    }

    /**
     * 无条件单表分页查询
     * @return
     */
    public ObjectPage<StudentTable> pageStudent() {
        return sessionTemplate.pageQuery(StudentTable.class);
    }

    /**
     * 使用 jql 查询
     * @return
     */
    public List<StudentTable> listStudent(String resId) {
        Jql jql = Jql.create()
                .select(stuTable.studentName)
                .from(stuTable)
                .where(stuTable.resId.eq(resId));
        return sessionTemplate.query(jql, StudentTable.class);
    }

}
